package org.rslai.autotest.core.driver.chrome;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.rslai.autotest.core.driver.AbstractDriver;
import org.rslai.autotest.core.driver.RemoteWebDriverForAutoTest;

/**
 * Chrome远程驱动
 * @author rslai
 */
public class RemoteChromeDriver extends AbstractDriver {

	@Override
	protected void startDriver() throws MalformedURLException {
		DesiredCapabilities capability = DesiredCapabilities.chrome();
		try {
			driver = new RemoteWebDriverForAutoTest(new URL(remoteAddress), capability);
		} catch (UnreachableBrowserException e) {
			UnreachableBrowserException exception = new UnreachableBrowserException("无法启动一个新会话。可能是无效的远程服务器的地址或浏览器启动失败。1、检查是否启动Selenium Server；2、检查防火墙设置。" + e.getMessage());
			exception.printStackTrace();
			throw exception;
		} catch (WebDriverException e) {
			WebDriverException exception = new WebDriverException("无法启动 ChromeDriver。1、检查ChromeDriver是否在系统path目录中；2、在环境变量中添加 webdriver.chrome.driver；3、重新下载ChromeDriver。" + e.getMessage());
			exception.printStackTrace();
			throw exception;
		}

		driver.manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);
		if (maximize) {
			driver.manage().window().maximize();
		}
	}

}
